package com.example.springbootexamples.controller;

import com.example.springbootexamples.domain.T1;
import com.example.springbootexamples.domain.User;
import com.example.springbootexamples.mapper.UserMapper;
import com.example.springbootexamples.repository.T1Repository;
import com.example.springbootexamples.service.T1Service;
import com.example.springbootexamples.service.T2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api("test")
@RestController
public class TestController {

    Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private T1Repository t1Repository;
    @Autowired
    private T1Service t1Service;
    @Autowired
    private T2Service t2Service;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Value("${a.b.c}")
    private String vv;

    @RequestMapping(value = "t",method = RequestMethod.GET)
    public Object t(@ApiParam("p")String p){
        //List<User> userList=userMapper.selectList(null);
        //logger.info("{}",userList.size());

        logger.info("vv = {}",vv);

        return "success";
    }

    @RequestMapping("tt")
    public Object tt(String a,String b){
        logger.info("a {} b {}",a,b);
        return a+b;
    }

    @RequestMapping(value="plus",method = RequestMethod.GET)
    public Object o(){
        userMapper.selectList(null);
        return "a";
    }

    Object redis(String user,String pass){
        logger.info("{} : {}",user,pass);
        if(user.equals("admin")&&pass.equals("admin")){
            stringRedisTemplate.delete(user+":"+"fail");
        }else{
            int n=0;
            String v= stringRedisTemplate.opsForValue().get(user+":fail");
            if(v==null){
                n=1;
            }else{
                n=Integer.parseInt(v);
                n++;
            }
            if(n==1){
                stringRedisTemplate.opsForValue().set(user+":"+"fail",n+"",10, TimeUnit.MINUTES);
            }else{
                Long expire=stringRedisTemplate.getExpire(user+":"+"fail");
                stringRedisTemplate.opsForValue().set(user+":"+"fail",n+"",expire,TimeUnit.SECONDS);
            }
            if(n>=3){
                stringRedisTemplate.opsForValue().set(user+":lock",user);
                throw new RuntimeException("lock");
            }
            throw new RuntimeException("fail "+n);
        }
        // stringRedisTemplate.opsForValue().set("a","1");
        logger.info("success");
        return null;
    }



    @RequestMapping(value = "t1")
    public Object t(Map<String,Object> map){
        logger.info("deal {}",map);
        t2Service.save(new ArrayList<>());
        return "a";
    }

    @RequestMapping(value = "testRepo")
    public Object testRepo(){



        return "oo";
    }


}
